package com.ssm.controllor;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.util.qrcode.GoogleQrcodeUtil;
import com.ssm.util.qrcode.QRCodeUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@SuppressWarnings("restriction")
@Controller  
@RequestMapping("/qrcode")  
public class QrcodeController {

	final Logger logger = LoggerFactory.getLogger(QrcodeController.class);
	
	//访问路径 http://localhost:8080/Util/qrcode/qrcode1
	//页面路径 http://localhost:8080/Util/qrcode/qrcode.jsp
    @RequestMapping(value = "qrcode1")
    public void qrcode1(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    	try {
    		String url="http://www.baidu.com";
			resp.setContentType("image/gif");
			ServletOutputStream out = resp.getOutputStream();
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			BufferedImage image = QRCodeUtil.createQRCode(url);
			encoder.encode(image);
			out.close();
		} catch (Exception e) {
			logger.error("产生二维码图片出现异常", e);
		}
        logger.info("这是一条日志信息 - {}", "qrcode1");
    }
    //访问路径 http://localhost:8080/Util/qrcode/qrcode2
  	//页面路径 http://localhost:8080/Util/qrcode/qrcode.jsp
    @RequestMapping(value = "qrcode2")
    public void qrcode2(HttpServletRequest req, HttpServletResponse resp) throws Exception {

      	try {
      		String url="http://www.baidu.com";
      		String path = this.getClass().getResource("/").toString();
      		String substring = path.substring(6,path.indexOf("WEB-INF"));
      		String logoPath = substring+"image/qrcode/222.jpg";
//      		String logoPath = "D:/222.jpg";
      		logger.error("logoPath：" +logoPath);
  			resp.setContentType("image/gif");
  			ServletOutputStream out = resp.getOutputStream();
  			
  			ByteArrayOutputStream logoQRCode = GoogleQrcodeUtil.getLogoQRCode(url, "跳转到百度的二维码",logoPath);
  			out.write(logoQRCode.toByteArray());
  			out.close();
  		} catch (Exception e) {
  			logger.error("产生二维码图片出现异常", e);
  		}
          logger.info("这是一条日志信息 - {}", "qrcode2");
    }
}
